<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<p id="description"></p>
<pre id="console"></pre>
<script>
description('Tests that using location.replace to update the hash of a page preserves it when navigating back to it.');

jsTestIsAsync = true;

var testWindow;

onload = function()
{
    if (window.testRunner) {
        testRunner.setPopupBlockingEnabled(false);
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
    }

    testWindow = window.open('resources/location-replace-hash-1.html');
    if (!testWindow)
        testFailed('Could not open test window');
}

var currentState = 0;
var currentPageId;

function onTestWindowNavigation(pageId)
{
    // The page ID is put in a global so that the eval() inside of shouldBe can
    // see it
    currentPageId = pageId;
    currentState++;

    switch (currentState) {
      case 1:
          shouldBe('currentPageId', '1');
          testWindow.location.replace('#foo');
          break;
      case 2:
          shouldBe('currentPageId', '1');
          shouldBe('testWindow.location.hash', '"#foo"');
          testWindow.location = 'location-replace-hash-2.html';
          break;
      case 3:
          shouldBe('currentPageId', '2');
          testWindow.history.back();
          break;
      case 4:
          shouldBe('currentPageId', '1');
          shouldBe('testWindow.location.hash', '"#foo"');
          break;
      default:
          testFailed('Should not be in state ' + currentState);
          break;
    }

    if (currentState == 4) {
        testPassed('Complete: navigated through all the states');
        finishJSTest();
    }
}
</script>
</body>
